package com.xsp.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.xsp.commonutils.servicebase.exceptionhandler.GuLiException;
import com.xsp.vod.service.VodService;
import com.xsp.vod.utils.ConstantVodUtils;
import com.xsp.vod.utils.InitObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    //上传视频，返回视频id
    @Override
    public String uploadVideo(MultipartFile file) throws IOException{
        //获取原始文件名
        String fileName = file.getOriginalFilename();
        //获取文件名，去掉后缀，做为上传的视频名字
        String title = fileName.substring(0, fileName.lastIndexOf("."));
        InputStream inputStream = file.getInputStream();
        //保存视频id
        String videoId;
        UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        if (response.isSuccess()) {
            videoId = response.getVideoId();
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            videoId = response.getVideoId();
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        inputStream.close();
        return videoId;
    }

    //根据视频id删除视频
    @Override
    public void removeVideo(String videoId) throws ClientException{
        //创建初始化对象
        DefaultAcsClient client  = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET);
        //获取request对象
        DeleteVideoRequest request = new DeleteVideoRequest();
        //设置videoId
        request.setVideoIds(videoId);
        //创建response对象，放入request
        DeleteVideoResponse response = client.getAcsResponse(request);
    }

    //批量删除视频
    @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建请求对象
            //一次只能批量删20个
            //toArray()将List转换成数组，join()将数组内元素用逗号进行连接
            String str = StringUtils.join(videoIdList.toArray(), ",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (ClientException e) {
            throw new GuLiException(20001, "视频删除失败");
        }
    }
}
